package wangguolongTest;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lancoo.znbkxx.R;
import com.lancoo.znbkxx.beans.FirstPageBean;
import com.lancoo.znbkxx.uis.PhoneMainActivity;

public class FirstPageBkfxFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_revision, container, false);
        return view;
    }

    private FirstPageBean FirstPageEntity;
    public void setFirstPagEntity(FirstPageBean bean){
        FirstPageEntity = bean;
    }

    private TextView mTvBai,mTvShi,mTvGe;
    private TextView mTvTotalDays;
    private TextView mTvFinishCount;
    private TextView mTvTotalCount;
    private TextView mTvAverage;
    private TextView mTvHighest;
    private TextView mTvLowest;
    private RecyclerView mRvFirstPageBkfx;
    private ImageView mIvNoPaperList;
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mTvBai = view.findViewById(R.id.tv_bai);
        mTvShi = view.findViewById(R.id.tv_shi);
        mTvGe = view.findViewById(R.id.tv_ge);

        mTvTotalDays = view.findViewById(R.id.tv_total_day);
        mTvFinishCount = view.findViewById(R.id.tv_finish_count);
        mTvTotalCount = view.findViewById(R.id.tv_total_count);
        mTvAverage = view.findViewById(R.id.tv_average);
        mTvHighest = view.findViewById(R.id.tv_highest);
        mTvLowest = view.findViewById(R.id.tv_lowest);

        mRvFirstPageBkfx = view.findViewById(R.id.rv_group);
        mRvFirstPageBkfx.setLayoutManager(new LinearLayoutManager(getActivity()));//必须先设置LayoutManager

        mIvNoPaperList = view.findViewById(R.id.iv_noPaperList);
        mIvNoPaperList.setVisibility(View.GONE);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ((PhoneMainActivity)getActivity()).geBaseInfo();
    }

    public void setList(final FirstPageBean bean){
        final Handler mH = new Handler();
        mH.post(new Runnable() {
            @Override
            public void run() {

                if(bean!=null){
                    BkfxItemAdapter mAdapter = new BkfxItemAdapter(getActivity(),bean);
                    mAdapter.setHasStableIds(true);
                    mRvFirstPageBkfx.setAdapter(mAdapter);
                    int ItemSize  = bean.getPaperList().size();
                    mRvFirstPageBkfx.setItemViewCacheSize(ItemSize);
                    Log.e("==bkfxFragment==","=============entity传输成功===================");
                    showPaperView(bean);
                    mH.removeCallbacks(this);
                }else{
                    Log.e("==bkfxFragment==","===========entity传输---失败====================");
                    if(mIvNoPaperList.getVisibility()==View.GONE){
                        mIvNoPaperList.setVisibility(View.VISIBLE);
                        mH.removeCallbacks(this);
                    }
                    mH.post(this);
                }
            }
        });
    }

    private void showPaperView(FirstPageBean bean){

        int PaperLeftDays = bean.getLeftDays();
        if (PaperLeftDays > 99){
            mTvBai.setText(String.valueOf(PaperLeftDays).charAt(0)+"");
            mTvShi.setText(String.valueOf(PaperLeftDays).charAt(1)+"");
            mTvGe.setText(String.valueOf(PaperLeftDays).charAt(2)+"");
        }else if (PaperLeftDays > 9 && PaperLeftDays < 100){
            mTvBai.setVisibility(View.INVISIBLE);
            mTvShi.setText(String.valueOf(PaperLeftDays).charAt(0)+"");
            mTvGe.setText(String.valueOf(PaperLeftDays).charAt(1)+"");
        }else {
            mTvBai.setVisibility(View.INVISIBLE);
            mTvShi.setVisibility(View.INVISIBLE);
            mTvGe.setText(String.valueOf(PaperLeftDays).charAt(0)+"");
        }

        mTvTotalDays.setText(bean.getTotalDays()+"");
        mTvFinishCount.setText(bean.getFinishCount()+"");
        mTvTotalCount.setText("/"+bean.getTotalCount()+"");
        mTvAverage.setText(Math.round(bean.getAverageScore())+"");
        mTvHighest.setText(Math.round(bean.getHighestScore())+"");
        mTvLowest.setText(Math.round(bean.getLowestScore())+"");


        mIvNoPaperList.setVisibility(View.GONE);//隐藏无试卷列表占位图
    }

}
